package com.yiwei.hdb.activity;

import android.os.Bundle;
import android.support.v4.content.ContextCompat;
import android.text.TextUtils;
import android.view.View;
import android.webkit.WebView;
import android.widget.FrameLayout;
import android.widget.ScrollView;
import android.widget.TextView;


import com.yiwei.hdb.R;
import com.yiwei.hdb.api.callback.AboutContract;
import com.yiwei.hdb.api.requestInfo.AboutRequest;
import com.yiwei.hdb.constant.Const;

import com.yiwei.hdb.model.ContentDetailModel;
import com.yiwei.hdb.model.ContractModel;

import com.yiwei.hdb.view.CircularProgress;

/**
 * 关于界面
 * Created by Master.Dong on 16/4/10.
 */
public class AboutActivity extends BaseActivity implements AboutContract.View {

    private static final int DEFAULT_TYPE = 0;
    private static final int CONTRACT_TYPE = 1;
    private static final int WEITUO_TYPE = 2;
    private TextView title, content;
    private CircularProgress loading;
    //    private WebView webView;
    private ScrollView sv;
    private int id;
    private int typeId;
    private AboutRequest api;
    private FrameLayout mWebContainer;
    private WebView mWebView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setBaseContentView(R.layout.about_activity);
        baseInitView(this, "AboutActivity", Const.ViewType.ACTIVITY);
        initView();
        setIsNeedCheckLogout(false);
    }

    private void initView() {
        api = AboutRequest.create();
        api.attachView(this);
        id = getIntent().getIntExtra("id", 0);
        int type = getIntent().getIntExtra("type", 0);
        typeId = getIntent().getIntExtra("type_id", 0);

        String bTitle = getIntent().getStringExtra("title");
        String borrow_amount = getIntent().getStringExtra("borrow_amount");
        String borrow_limit_day = getIntent().getStringExtra("borrow_limit_day");
        String borrow_apr = getIntent().getStringExtra("borrow_apr");
        String service_fee = getIntent().getStringExtra("service_fee");
        String bankcard_id = getIntent().getStringExtra("bankcard_id");
        if (TextUtils.isEmpty(bTitle))
            bTitle = getResources().getString(R.string.title_bar_name);

        setTitleText(bTitle);
        setBackIntent();

        sv = (ScrollView) findViewById(R.id.sv);
        loading = (CircularProgress) findViewById(R.id.progressBar);
        title = (TextView) findViewById(R.id.title);
        content = (TextView) findViewById(R.id.about_content);
        mWebContainer = (FrameLayout) findViewById(R.id.webview);
        mWebView = new WebView(getApplicationContext());
        mWebContainer.addView(mWebView);
        switch (type) {
            case DEFAULT_TYPE:
                sv.setBackgroundColor(ContextCompat.getColor(getApplicationContext(), R.color.white));
                content.setVisibility(View.GONE);
                getAbout();
                break;
            case CONTRACT_TYPE:
                content.setVisibility(View.GONE);
                getContract(id);
                break;
            case WEITUO_TYPE:
                if (id == Const.ContractType.FUWU_HETONG) {
                    content.setVisibility(View.GONE);
                    api.getFuWuHeTong(id + "", borrow_amount, borrow_limit_day, borrow_apr, service_fee,bankcard_id);
                } else if (id == Const.ContractType.WEITUO) {
                    content.setVisibility(View.GONE);
                    api.getWeiTuo(id + "");
                }

                break;
        }

    }

    private void setWebView(String content) {
//        if (webView == null)
//            webView = (WebView) findViewById(R.id.webview);

        mWebContainer.setVisibility(View.VISIBLE);
        mWebView.setBackgroundColor(0);

        mWebView.getSettings().setJavaScriptEnabled(true);
        mWebView.getSettings().setDefaultTextEncodingName("utf-8");

        String html = "<html><head><title>服务协议</title></head><body></body></html>";
        mWebView.loadDataWithBaseURL(html, content + "", "text/html", "utf-8", null);
    }


    /**
     * 获取内容
     */
    private void getAbout() {
        api.getAbout();
    }


    /**
     * 获取协议
     *
     * @param id
     */
    private void getContract(int id) {
        api.getContractInfo(String.valueOf(id), typeId + "", "");
    }


    /**
     * 是否显示加载框
     *
     * @param b
     */
    private void showLoading(boolean b) {
        if (loading != null) {
            if (b)
                loading.setVisibility(View.VISIBLE);
            else
                loading.setVisibility(View.GONE);
        }
    }

    @Override
    public void showError() {

    }

    @Override
    public void complete() {
        showLoading(false);
    }

    @Override
    public void onRequestStart() {
        showLoading(true);
    }


    @Override
    public void getAboutSuccess(ContentDetailModel item) {
        if (!TextUtils.isEmpty(item.getTitle())) {
            setTitleText(item.getTitle());
        }
        if (!TextUtils.isEmpty(item.getContent())) {
            setWebView(item.getContent());
        }
    }

    @Override
    public void getContractSuccess(ContractModel item) {
        setTitleText(item.getData().getTitle());
        setWebView(item.getData().getContent());
    }

    @Override
    public void getWeiTuo(ContractModel item) {
        setTitleText(item.getData().getTitle());
        setWebView(item.getData().getContent());
    }

    @Override
    public void getFuWuHeTong(ContractModel item) {
        setTitleText(item.getData().getTitle());
        setWebView(item.getData().getContent());
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        api.detachView();
        api = null;
        mWebContainer.removeAllViews();
        if (mWebView != null) {
            mWebView.onPause();
            mWebView.destroy();
            mWebView = null;
        }
    }
}
